package com.bifang.core.dict.gateway.controller;

import cn.hutool.core.convert.Convert;
import com.bifang.common.constant.Validators;
import com.bifang.common.vo.AppResponse;
import com.bifang.common.vo.CommonDeleteVO;
import com.bifang.common.vo.CommonDetailVO;
import com.bifang.common.vo.CommonVO;
import com.bifang.core.dict.gateway.req.CommonClassReq;
import com.bifang.core.dict.gateway.req.CommonDeleteReq;
import com.bifang.core.dict.gateway.req.CommonDetailReq;
import com.bifang.core.dict.gateway.req.CommonKeyReq;
import com.bifang.core.dict.gateway.req.CommonReq;
import com.bifang.core.dict.integration.CommonIntegration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/system/params")
@Api(tags = "系统参数管理")
public class SystemParamController {

    @Autowired private CommonIntegration commonIntegration;
    /**
     * 系统参数查询-分页
     *
     * @return
     */
    @PostMapping("/query/list")
    @ApiOperation(value = "系统参数查询", notes = "系统参数查询-分页")
    public AppResponse getCommonList(@RequestBody CommonReq commonReq) {
        CommonVO commonVO = Convert.convert(CommonVO.class, commonReq);
        return AppResponse.successData(commonIntegration.getCommonList(commonVO));
    }

    /**
     * 系统参数详情获取
     *
     * @return
     */
    @PostMapping("/query/detail")
    @ApiOperation(value = "系统参数详情获取", notes = "系统参数详情获取")
    public AppResponse getSystemParamDetail(
            @RequestBody @Validated CommonDetailReq commonDetailReq) {
        CommonDetailVO commonDetailVO = Convert.convert(CommonDetailVO.class, commonDetailReq);
        return AppResponse.successData(commonIntegration.getSystemParamDetail(commonDetailVO));
    }
    /** 新增 */
    @ApiOperation(value = "系统参数新增", notes = "系统参数新增")
    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")
    @Transactional
    public AppResponse add(
            @RequestBody @Validated(value = {Validators.Add.class}) CommonReq commonReq) {
        CommonVO commonVo = Convert.convert(CommonVO.class, commonReq);
        return AppResponse.successData(commonIntegration.add(commonVo));
    }
    /** 修改 */
    @ApiOperation(value = "系统参数修改", notes = "系统参数修改")
    @PostMapping(value = "/edit", produces = "application/json;charset=UTF-8")
    @Transactional
    public AppResponse edit(@RequestBody @Validated CommonReq commonReq) {
        CommonVO commonVo = Convert.convert(CommonVO.class, commonReq);
        return AppResponse.successData(commonIntegration.edit(commonVo));
    }
    /** 删除 */
    @ApiOperation(value = "系统参数删除", notes = "系统参数删除")
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    @Transactional
    public AppResponse delete(@RequestBody CommonDeleteReq commonReq) {
        CommonDeleteVO commonDeleteVO = Convert.convert(CommonDeleteVO.class, commonReq);
        commonIntegration.delete(commonDeleteVO);
        return AppResponse.success();
    }
    /** 删除 批量 */
    @ApiOperation(value = "系统参数删除批量", notes = "系统参数删除批量")
    @PostMapping(value = "/delete/batch", produces = "application/json;charset=UTF-8")
    @Transactional
    public AppResponse deleteBatch(@RequestBody CommonDeleteReq commonReq) {
        CommonDeleteVO commonDeleteVO = Convert.convert(CommonDeleteVO.class, commonReq);
        commonIntegration.deleteBatch(commonDeleteVO);
        return AppResponse.success();
    }
    /** 清除缓存 */
    @ApiOperation(value = "系统参数清除缓存", notes = "系统参数清除缓存")
    @PostMapping(value = "/clear/cache", produces = "application/json;charset=UTF-8")
    public AppResponse clearCache(@RequestBody CommonReq commonReq) {
        CommonVO commonVO = Convert.convert(CommonVO.class, commonReq);
        commonIntegration.clearCache(commonVO);
        return AppResponse.success();
    }

    @ApiOperation(value = "根据key获取值", notes = "根据key获取值")
    @PostMapping(value = "/getValue/byKey", produces = "application/json;charset=UTF-8")
    @Transactional
    public AppResponse getValueByKey(@RequestBody @Validated CommonKeyReq commonKeyReq) {

        return AppResponse.successData(commonIntegration.getValueByKey(commonKeyReq.getParamKey()));
    }

    @ApiOperation(value = "根据key和分类获取值", notes = "根据key和分类获取值")
    @PostMapping(value = "/getValue/byKeyAndClassify", produces = "application/json;charset=UTF-8")
    @Transactional
    public AppResponse getValueByKeyAndClassify(
            @RequestBody @Validated CommonClassReq commonClassReq) {
        return AppResponse.successData(
                commonIntegration.getValueByKeyAndClassify(
                        commonClassReq.getParamKey(), commonClassReq.getClassify()));
    }
}
